﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GearBehaviour : UnitBehaviour
{
    public Gear Gear
    {
        get { return Unit as Gear; }
        set { Unit = value; }
    }

    public override void Init()
    {
        base.Init();
        var g = Unit as Gear;
        var cnt = 9 + (int)(9 * g.Radius);

        var goe = NewChildObj("edge");
        var goc = NewChildObj("center");
        var clr = goc.AddComponent<LineRenderer>();
        var lr = goe.AddComponent<LineRenderer>();
        var pts = new Vector3[cnt];
        var ptsCenter = new Vector3[cnt];
        var a = 0f;
        var da = Mathf.PI * 2 / pts.Length;
        for (var i = 0; i < cnt; i++)
        {
            var r = g.GetRadius(a);
            var ir = new Vector3(Mathf.Cos(a), Mathf.Sin(a), 0);
            pts[i] = ir * r;
            ptsCenter[i] = ir * 0.2f;
            a += da;
        }

        lr.loop = true;
        lr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
        lr.startWidth = lr.endWidth = 0.1f;
        lr.startColor = lr.endColor = Color.green;
        lr.material = DefaultMaterial;
        lr.useWorldSpace = false;
        lr.positionCount = pts.Length;
        lr.SetPositions(pts);

        clr.loop = true;
        clr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
        clr.startWidth = lr.endWidth = 0.1f;
        clr.startColor = clr.endColor = Color.green;
        clr.material = DefaultMaterial;
        clr.useWorldSpace = false;
        clr.positionCount = ptsCenter.Length;
        clr.SetPositions(ptsCenter);

        // 光滑轮
        if (!g.ClockwiseThooth && !g.CounterClockwiseThooth)
            return;

        var thoothSlope = 0f;
        if (g.ClockwiseThooth && !g.CounterClockwiseThooth)
            thoothSlope = -Mathf.PI / 3;
        else if (!g.ClockwiseThooth && g.CounterClockwiseThooth)
            thoothSlope = Mathf.PI / 3;

        a = 0f;
        for (var i = 0; i < cnt; i++)
        {
            if (a < g.ToothStartAngle || a > g.ToothEndAngle)
            {
                a += da;
                continue;
            }

            var r = g.GetRadius(a);
            var go = NewChildObj("t");
            lr = go.AddComponent<LineRenderer>();

            pts = new Vector3[2];
            var ts = Gear.Radius / 2 + 0.5f;
            pts[0] = new Vector3(thoothSlope == 0 ? -0.1f * ts : -0.2f * ts, 0, 0);
            pts[1] = new Vector3(thoothSlope == 0 ? 0.1f * ts : 0.2f * ts, 0, 0);
            go.transform.localPosition = new Vector3(Mathf.Cos(a) * r, Mathf.Sin(a) * r);
            go.transform.localRotation = Quaternion.Euler(0, 0, MathEx.Arc2Dir(a + thoothSlope));
            go.transform.localScale = Vector3.one;

            lr.loop = false;
            lr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            lr.startWidth = lr.endWidth = 0.1f;
            lr.startColor = Color.green;
            lr.endColor = Color.yellow;
            lr.material = DefaultMaterial;
            lr.useWorldSpace = false;
            lr.positionCount = pts.Length;
            lr.SetPositions(pts);
            a += da;
        }
    }
}
